package com.tiny.work.test.zookeeper;

import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.ZooKeeper;

import java.util.List;

public class UpdateNode {
    public static void main(String[] args) throws Exception{
        ZooKeeper newzk = new ZooKeeper("10.0.5.59:2181", 60000, null);
//        ZooKeeper newzk = new ZooKeeper("localhost:2181", 60000, null);
        //迁移的节点
        String node = "/goldfish";
        List<String> children = newzk.getChildren(node, false);
        move(newzk, children,node);
        newzk.close();
    }

    private static void move(ZooKeeper newzk, List<String> children, String parent) throws KeeperException, InterruptedException {
        if(children==null || children.isEmpty()){
            return;
        }else{
            for(String child:children){
                String c = parent+"/"+child;
                System.out.println(c);
                byte[] data = "{\"id\":\"10.0.5.59:26379\",\"masterName\":\"mymaster\",\"maxIdle\":10,\"maxTotal\":50,\"maxWaitMillis\":3000,\"minIdle\":0,\"sentinels\":[\"10.0.5.59:26379\"],\"testOnBorrow\":false,\"testOnReturn\":false,\"testWhileIdle\":false}".getBytes();
                if (child.equals("client")){
                    newzk.setData(c, data, -1, null, null);
                }
                move(newzk, newzk.getChildren(c, false),c);
            }
        }
    }
}
